package service;

import domain.Player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import static events.Events.deathEvents;
import static ui.SelectAttributesJFrame.player;

public class GameService {
    /*
       功能 根据五项属性（IQ、PF、FS、FFS、Luck）选择触发“高属性”或“低属性”事件。
       参数: Player player
    */
    /* 返回值说明：
     *   -1  ：无特定事件发生，五项属性均不满足“≤3”或“≥8”条件
     *   0  ：触发“低 IQ”事件
     *   10  ：触发“低 PF”事件
     *   20  ：触发“低 FS”事件
     *   30  ：触发“低 FFS”事件
     *   40  ：触发“低 Luck”事件
     *
     *  100  ：触发“高 IQ”事件
     *    1  ：触发“高 PF”事件
     *    2  ：触发“高 FS”事件
     *    3  ：触发“高 FFS”事件
     *    4  ：触发“高 Luck”事件
     * */


    public static int choiceWhichEvent(Player player) {
        int iq = player.getIq();
        int pf = player.getPf();
        int fs = player.getFs();
        int ffs = player.getFfs();
        int luck = player.getLuck();

        boolean[] booleans = new boolean[10];
        String[] att = {"iq", "pf", "fs", "ffs", "luck"};
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("iq", iq);
        hashMap.put("pf", pf);
        hashMap.put("fs", fs);
        hashMap.put("ffs", ffs);
        hashMap.put("luck", luck);
        int index = 0;
        for (String key : att) {
            int val = hashMap.get(key);
            booleans[index++] = val <= 3;
            booleans[index++] = val >= 8;
        }
        ArrayList<Integer> resultSet = new ArrayList<>();
        for (int i = 0; i < booleans.length; i++) {
            if (booleans[i]) {
                resultSet.add(i);
            }
        }
        if (resultSet.isEmpty()) {
            //Boolean数组全为false的情况下不发生特定事件，此时结果结合为空
            return -1;
        }
        Random rand = new Random();
        //生成下标随机数，在结果集中随机抽一个下标
        int i = rand.nextInt(resultSet.size());
        int temp = resultSet.get(i);
        int finalTemp = temp / 2;

        if (temp % 2 != 0) {
            //奇数,返回高属性事件
            if (finalTemp == 0) {
                //高iq
                return 100;
            }
            if (finalTemp == 1) {
                //高pf
                return 1;
            }
            if (finalTemp == 2) {
                //高fs
                return 2;
            }
            if (finalTemp == 3) {
                //高ffs
                return 3;
            }
            if (finalTemp == 4) {
                //高luck
                return 4;
            }
        } else {
            //偶数,返回低属性事件
            if (finalTemp == 0) {
                //低iq
                return 0;
            }
            if (finalTemp == 1) {
                //低pf
                return 10;
            }
            if (finalTemp == 2) {
                //低fs
                return 20;
            }
            if (finalTemp == 3) {
                //低ffs
                return 30;
            }
            if (finalTemp == 4) {
                //低luck
                return 40;
            }

        }
        return -1;
    }

    public static int choiceWhichDeathEvent(Player player) {
        final int Death = -1;
        final int EVENT_HIGH_IQ = 100;
        final int EVENT_HIGH_PF = 200;
        final int EVENT_HIGH_FS = 300;
        final int EVENT_HIGH_FFS = 400;
        final int EVENT_HIGH_LUCK = 500;
        final int EVENT_LOW_IQ = 101;
        final int EVENT_LOW_PF = 201;
        final int EVENT_LOW_FS = 301;
        final int EVENT_LOW_FFS = 401;
        final int EVENT_LOW_LUCK = 501;

        int iq = player.getIq();
        int luck = player.getLuck();
        int pf = player.getPf();
        int fs = player.getFs();
        int ffs = player.getFfs();

        Random rand = new Random();
        ArrayList<Integer> list = new ArrayList<>();
        if (iq <= 3) {
            list.add(EVENT_LOW_IQ);
        }
        if (iq >= 8) {
            list.add(EVENT_HIGH_IQ);
        }
        if (luck <= 3) {
            list.add(EVENT_LOW_LUCK);
        }
        if (luck >= 8) {
            list.add(EVENT_HIGH_LUCK);
        }
        if (pf <= 3) {
            list.add(EVENT_LOW_PF);
        }
        if (pf >= 8) {
            list.add(EVENT_HIGH_PF);
        }
        if (ffs <= 3) {
            list.add(EVENT_LOW_FFS);
        }
        if (ffs >= 8) {
            list.add(EVENT_HIGH_FFS);
        }
        if (fs <= 3) {
            list.add(EVENT_LOW_FS);
        }
        if (fs >= 8) {
            list.add(EVENT_HIGH_FS);
        }
        if (list.isEmpty()) {
            return Death;
        }
        int index = rand.nextInt(list.size());
        return list.get(index);
    }

    public static String choiceDeath() {
        int temp = choiceWhichDeathEvent(player);
        Random rand = new Random();
        int i = rand.nextInt(4 + 1);
        switch (temp) {
            case 100:
                return deathEvents[1][i];
            case 101:
                return deathEvents[0][i];
            case 201:
                return deathEvents[2][i];
            case 200:
                return deathEvents[3][i];
            case 300:
                return deathEvents[5][i];
            case 301:
                return deathEvents[4][i];
            case 400:
                return deathEvents[7][i];
            case 401:
                return deathEvents[6][i];
            case 500:
                return deathEvents[9][i];
            case 501:
                return deathEvents[8][i];

        }
        return deathEvents[10][i];
    }

    public static void deathAge(Player player) {
        Random random = new Random();
        //属性为零就在0岁死
        if (player.getFfs() == 0 && player.getPf() == 0
                && player.getFs() == 0 && player.getLuck() == 0 && player.getIq() == 0) {
            player.setDeathAge(0);
            return;
        }
        if (player.getPf() == 0) {
            player.setDeathAge(0);
            return;
        }
        // 寿命极短  0~15 岁
        if (player.getLuck() <= 2 && player.getPf() <= 2 && player.getFfs() <= 2) {
            player.setDeathAge(random.nextInt(15 + 1));
            return;
        }

        // 寿命较短  0~15 岁
        if (player.getLuck() <= 3 && player.getPf() <= 3 && player.getFfs() <= 3) {
            player.setDeathAge(random.nextInt(21));
            return;
        }

        // 寿命中等偏短  30~50 岁
        if (player.getLuck() <= 3 && player.getPf() <= 3) {
            player.setDeathAge(random.nextInt(21) + 30);
            return;
        }

        // 普通情况   55~70 岁
        if ((player.getLuck() <= 3 || player.getFfs() <= 3) && player.getPf() >= 4) {
            player.setDeathAge(random.nextInt(16) + 55);
            return;
        }

        // 正常随机情况 60~99 岁
        player.setDeathAge(random.nextInt(40) + 60);
    }

}

